package cn.youframe.frame.dao;

import cn.youframe.frame.base.BaseDaoImpl;
import cn.youframe.frame.beans.CommTypeVO;
import cn.youframe.frame.beans.CommTypeValVO;
import cn.youframe.frame.util.SqlBuilder;
import org.springframework.jdbc.core.BeanPropertyRowMapper;

import java.util.List;

/**
 * User: Administrator
 * DateTime: 13-2-18: 下午4:08
 */
public class DictDaoImpl extends BaseDaoImpl implements DictDao {
    @Override
    public List<CommTypeValVO> queryDictVals() {
        SqlBuilder sbSql = new SqlBuilder();
        sbSql.append("select VSEQ,TYPE_ID,VAL_ID,VAL_VAL,PVAL_ID,");
        sbSql.append("VAL_NAME,EXT_VAL,PRE1,PRE2,PRE3,ENABLE,MEMO");
        sbSql.append(" from YF_COMM_TYPE_VAL where ENABLE='0'");
        List<CommTypeValVO> dicList=getJdbcTemplate().query(sbSql.getSQL(), BeanPropertyRowMapper.newInstance(CommTypeValVO.class));
        return dicList;
    }

    @Override
    public List<CommTypeVO> queryTypes() {
        List<CommTypeVO> dicType=getJdbcTemplate().query("select * from YF_COMM_TYPE", BeanPropertyRowMapper.newInstance(CommTypeVO.class));
        return dicType;
    }

    @Override
    public int addType(CommTypeVO typeVO) {
        SqlBuilder sqlb=new SqlBuilder();
        sqlb.append("insert into YF_COMM_TYPE(TYPE_ID,TYPE_NAME,TYPE_KIND,MEMO)");
        sqlb.append(" values(?,?,?,?)");
        sqlb.addArg(typeVO.getTypeId()).addArg(typeVO.getTypeName());
        sqlb.addArg(typeVO.getTypeKind()).addArg(typeVO.getMemo());
        return getJdbcTemplate().update(sqlb.getSQL(),sqlb.getArgArray());
    }

    @Override
    public int updateType(CommTypeVO typeVO,String oldTypeId) {
        SqlBuilder sqlb=new SqlBuilder();
        sqlb.append("update YF_COMM_TYPE set TYPE_NAME=?,TYPE_KIND=?,MEMO=? ");
        sqlb.addArg(typeVO.getTypeName()).addArg(typeVO.getTypeKind()).addArg(typeVO.getMemo());
        if(!oldTypeId.equalsIgnoreCase(typeVO.getTypeId())){
            sqlb.append(",TYPE_ID=?").addArg(typeVO.getTypeId());
        }
        sqlb.append(" where TYPE_ID=?");
        sqlb.addArg(oldTypeId);
        return getJdbcTemplate().update(sqlb.getSQL(),sqlb.getArgArray());
    }

    @Override
    public int updateTypeVals(String newTypeId,String oldTypeId) {
        SqlBuilder sqlb=new SqlBuilder();
        sqlb.append("update YF_COMM_TYPE_VAL set TYPE_ID=? where TYPE_ID=? ");
        sqlb.addArg(newTypeId).addArg(oldTypeId);
        return getJdbcTemplate().update(sqlb.getSQL(),sqlb.getArgArray());
    }

    @Override
    public int delType(String typeId) {
        return getJdbcTemplate().update("delete YF_COMM_TYPE where TYPE_ID=?",typeId);
    }

    @Override
    public int delTypeVals(String typeId) {
        return getJdbcTemplate().update("delete YF_COMM_TYPE_VAL where TYPE_ID=?",typeId);
    }
}
